var gridList,
    userId;

var form
    , layer
    , layedit
    , laydate
    , element;

$(document).ready(function () {
    UserInfo.init();
    userid = UserInfo.userId;
    console.log(UserInfo)
    gridList = new dataGrid("gridList");
    layui.use(['form', 'layedit', 'laydate', 'element'], function () {
        form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , element = layui.element;

        laydate.render({
            elem: '#beginTime' //开始时间
        });
        laydate.render({
            elem: '#endTime'   //结束时间
        });

    });
    init_Data();
});

function init_Data() {
    gridList.load({'consignor':userid,'mandatary':userid});
}


function formatOper(e) {
    var row = e.record;
    return "<a class='layui-btn layui-btn-xs st_back3' lay-event='edit' onClick='editData(\"" + e.value + "\")'><i class='layui-icon'>&#xe642;</i>编辑</a> " +
        "<a class='layui-btn layui-btn-danger layui-btn-xs st_back4' lay-event='del' onClick='removeData(\"" + e.value + "\")'><i class='layui-icon'>&#xe640;</i>结束委托</a>";
}

function addData() {
    parent.layer.open({
        type: 2,
        title: '工作委托',
        area: ['750px', '600px'],
        fixed: false, //不固定
        shadeClose: true,
        maxmin: true,
        content: "base/workFlow/wfMyWork/entrustDetail.html?type=new&temp=" + new Date().getUTCMilliseconds(),
        end: function () {
            gridList.reload();
        }
    });
}

function editData(id) {
    parent.layer.open({
        type: 2,
        title: '编辑工作委托',
        area: ['750px', '600px'],
        fixed: false, //不固定
        maxmin: true,
        shadeClose: true,
        content: "base/workFlow/wfMyWork/entrustDetail.html?type=edit&id=" + id + "&temp=" + new Date().getUTCMilliseconds(),
        end: function () {
            gridList.reload();
        }
    });
}

function removeData(id) {
    parent.layer.confirm('您确定要结束该委托吗？', {
        btn: ['确定', '取消'] //按钮
    }, function () {
        ActHttpRequest("/wf/entrust/del/" + id, {}, function (data) {
            if (data) {
                parent.layer.msg('操作成功！', {
                    icon: 1,
                    time: 1000 //2秒关闭（如果不配置，默认是3秒）
                }, function () {
                    gridList.reload();
                })
            } else {
                parent.layer.msg('操作失败！', {
                    icon: 1,
                    time: 1000 //2秒关闭（如果不配置，默认是3秒）
                }, function () {
                    gridList.reload();
                })
            }
        });
    }, function () {
    });
}

function formatConsignor(e) {
    var row = e.record;
    var consignor = "";
    ActHttpRequest("/sys-user/" + row.consignor, {}, function (d) {
        consignor = d.sysUser.userName;
    });
    return consignor;
}

function getEnState(e) {
    var row = e.record;
    var state = "";
    var startTime = new Date(row.beginTime);
    var endTime = new Date(row.endTime);
    var myDate = new Date();
    if (myDate.getTime()<startTime) {
        state="未开始";
    }
    else if(myDate.getTime()>endTime){
        state="已结束";
    }
    else {
        state="委托中";
    }
    return state;
};

function formatMandatary(e) {
    var row = e.record;
    var mandatary = "";
    ActHttpRequest("/sys-user/" + row.mandatary, {}, function (d) {
        mandatary = d.sysUser.userName;
    });
    return mandatary;
}

function formatWfId(e) {
    var row = e.record;
    var wfName = "";
    var str=row.wfId.split(",");
    if (str.length > 1) {
        for (var i = 0; i < str.length; i++) {
            ActHttpRequest("/wf/workflow/" + str[i], {}, function (d) {
                wfName = wfName + d.wfWorkflow.wfName + ",";
            });
        }
        wfName = wfName.substring(0, wfName.lastIndexOf(','));

    } else {
        ActHttpRequest("/wf/workflow/" + row.wfId, {}, function (d) {
            wfName = d.wfWorkflow.wfName;
        });
    }
    return wfName;
}
